package com.shopping.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopping.dao.impl.BizDaoImpl;
import com.shopping.entity.TbMember;
import com.shopping.service.impl.BizServiceImpl;

/**
 * Servlet implementation class fg_update_2_Servlet
 */
public class fg_update_2_Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public fg_update_2_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("gbk");
        response.setContentType("text/html");

        BizDaoImpl bizdao = new BizDaoImpl();
        BizServiceImpl bsi = new BizServiceImpl();
        bsi.setBizdao(bizdao);

        System.out.println("到此一游");

        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
        TbMember tb = (TbMember) session.getAttribute("TB");// 获取前面传过来的真实会员信息

        String oldPassword = request.getParameter("oldPassword");// 获取输入的原始密码
        String password = request.getParameter("password");// 输入的新密码
        String passwordOne = request.getParameter("passwordOne");// 第二次输入的新密码
        String reallyName = request.getParameter("reallyName");// 输入的真实姓名
        String num = request.getParameter("age");

        String profession = request.getParameter("profession");// 输入的职业
        String email = request.getParameter("email");// 输入的邮箱

        if (oldPassword.equals("") || password.equals("") || passwordOne.equals("") || reallyName.equals("")
                || num.equals("") || profession.equals("") || email.equals("")) {
            out.print("<script>");
            out.print("alert('表单信息不能为空');");
            out.print("window.location.href='fg-updateMember.jsp';");
            out.print("</script>");
            out.flush();
            out.close();
            return;
        } else {
            if (oldPassword.equals(tb.getPassword())) {
                if (oldPassword.equals(password)) {
                    out.print("<script>");
                    out.print("alert('新密码不能与原始密码一致');");
                    out.print("window.location.href='fg-updateMember.jsp';");
                    out.print("</script>");
                    out.flush();
                    out.close();
                    return;
                } else {
                    if (password.equals(passwordOne)) {
                        Integer age = Integer.parseInt(num);// 输入的年龄
                        tb.setPassword(password);
                        tb.setReallyName(reallyName);
                        tb.setAge(age);
                        tb.setProfession(profession);
                        tb.setEmail(email);
                        bsi.updatetbmemberone(tb);
                        out.print("<script>");
                        out.print("alert('会员修改成功');");
                        out.print("window.location.href='loginOut.jsp';");
                        out.print("</script>");
                        out.flush();
                        out.close();

                    } else {
                        out.print("<script>");
                        out.print("alert('您两次输入的密码不一致，请重新输入');");
                        out.print("window.location.href='fg-updateMember.jsp';");
                        out.print("</script>");
                        out.flush();
                        out.close();
                        return;
                    }
                }
            } else {
                out.print("<script>");
                out.print("alert('旧密码不正确');");
                out.print("window.location.href='fg-updateMember.jsp';");
                out.print("</script>");
                out.flush();
                out.close();
                return;
            }
        }

        // if (oldPassword.equals(tb.getPassword())) {// 输入的原始密码和真是原始密码进行比较
        //
        // if (password.equals(oldPassword)) {
        // request.setAttribute("flag_four", null);// 密码修改失败,新密码不可以与旧密码一致！！！
        // request.getRequestDispatcher("fg-four.jsp").forward(request,
        // response);
        // } else {
        //
        // request.setAttribute("flag_four", 1);// 修改成功
        // request.getRequestDispatcher("fg-four.jsp").forward(request,
        // response);
        //
        // }
        //
        // } else {
        // out.print("<script>");
        // out.print("alert('旧密码不正确!');");
        // out.print("window.location.href='fg-updateMember.jsp';");
        // out.print("</script>");
        // out.flush();
        // out.close();
        // }

    }
}
